///为.net 调用web server 并取得JSON的AJAX
///    <script src="/AGuang_Client/DocumentSize.js" type="text/javascript"></script>
///    <script src="/AGuang_Client/expand.js" type="text/javascript"></script>
///    <script src="/AGuang_Client/EventOperate.js" type="text/javascript"></script>

Function.registerNameSpace("AGuang");

AGuang.AjaxForJSON = function(){
    this.XMLHttp=this._createXMLHTTP();
    this._url=null;
    this._method=null;
    this._data=null;
    
    this._xmlHttpAction=Function.createDelegate(this,this._readystatechange);
}

AGuang.AjaxForJSON.UNINIT=0;
AGuang.AjaxForJSON.LOADING=1;
AGuang.AjaxForJSON.LOADED=2;
AGuang.AjaxForJSON.INTERACTIVE=3;
AGuang.AjaxForJSON.COMPLETE=4;

///队列
AGuang.AjaxForJSON._________AjaxQueue=[];

//是否在处理
AGuang.AjaxForJSON._________Process=false;

//真正的处理程序，按队列处理
AGuang.AjaxForJSON._______send=function(info){
	AGuang.AjaxForJSON._________Process=true;//表示正在工作
	try{

	    var url=info.get_url()+"/"+info.get_method();
	    info.XMLHttp.open("POST",url,true);
	    info.XMLHttp.onreadystatechange=info._xmlHttpAction;
	    //EventOperate.addEvents=EventOperate.addEvents(info.XMLHttp,info._xmlHttpAction);
	    info.XMLHttp.setRequestHeader("Content-Type", "application/json");
	    info.XMLHttp.send(info.get_data());
    }catch(en){
        var e=info._getInfo();    		    
	    var aHandler=EventOperate.getHandler(info,"error");
	    if(aHandler) aHandler(info,e);
	    else info._showError(e);
    }
    //执行队列中的下一人任务
    if(AGuang.AjaxForJSON._________AjaxQueue.length>0) AGuang.AjaxForJSON._______send(AGuang.AjaxForJSON._________AjaxQueue.shift());
    else AGuang.AjaxForJSON._________Process=false;//表示工作结束
}


AGuang.AjaxForJSON.prototype = {
    ///处理页面（web service的URL）
    set_url: function(value) { this._url = value; },
    get_url: function() { return this._url; },

    ///处理方法（web service的方法）
    set_method: function(value) { this._method = value; },
    get_method: function() { return this._method; },

    ///传送的数据
    set_data: function(value) { this._data = value; },
    get_data: function() { return this._data; },

    ///添加数据正确回传后的事件处理程序
    add_dataLoaded: function(handler) { EventOperate.addHandler(this, "dataLoaded", handler); },
    remove_dataLoaded: function(handler) { EventOperate.removeHandler(this, "dataLoaded", handler); },

    ///添加处理出错后的事件处理程序
    add_error: function(handler) { EventOperate.addHandler(this, "error", handler); },
    remove_error: function(handler) { EventOperate.removeHandler(this, "error", handler); },

    ///将设置的数据发前出去
    send: function() {
        //将任务加入队列
        AGuang.AjaxForJSON._________AjaxQueue.push(this);
        if (AGuang.AjaxForJSON._________Process == false) {//如果没有启动工作，则启动工作
            AGuang.AjaxForJSON._________Process = true;
            AGuang.AjaxForJSON._______send(AGuang.AjaxForJSON._________AjaxQueue.shift());
        }
    },

    //回传处理
    _readystatechange: function(sender, e) {
        if (this.XMLHttp.readyState == AGuang.AjaxForJSON.COMPLETE) {
            if (this.XMLHttp.status == 200 || this.XMLHttp.status == 0) {
                var e = this._getInfo();
                var aHandler = EventOperate.getHandler(this, "dataLoaded");
                if (aHandler) aHandler(this, e);
            } else {
                var e = this._getInfo();
                var aHandler = EventOperate.getHandler(this, "error");
                if (aHandler) aHandler(this, e);
                else this._showError(e);
            }
        }
    },

    _getInfo: function() {
        //事件传出值
        var e = {};
        e.readyState = this.XMLHttp.readyState;
        e.status = this.XMLHttp.status;
        e.statusText = this.XMLHttp.statusText;
        e.responseHeaders = this.XMLHttp.getAllResponseHeaders();
        e.responseText = this.XMLHttp.responseText;
        e.responseXML = this.XMLHttp.responseXML;
        e.responseJSON = function() {
            return (new Function("return " + e.responseText))();
        }
        return e;
    },

    _showError: function(e) {
        alert("程序处理错误！" +
		    "\n处理状态:" + e.readyState +
		    "\n服务器状态:" + e.status + " [" + e.statusText + "]" +
		    "\n文件头信息:" + e.responseHeaders + "");
    },

    _createXMLHTTP: function() {
        if (window.XMLHttpRequest) {
            var XMLHttp = new window.XMLHttpRequest();
            //XMLHttp.overrideMimeType("text/xml");
            return XMLHttp;
        }
        else if (window.ActiveXObject) {
            var XMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
            return XMLHttp;
        }
    }
}

